package dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.TextView;

import com.metodo_ahp.usuario.aplicacionmetodoahp.R;

import adapters.CriterioDefinicionAdapter;
import adapters.ExpListAdapterCompararAlternativas;
import gestores.GestorModeloAHP;
import modelo.Item;
import steps.step_1_def_criterios;

/**
 * Created by Usuario on 19/10/2014.
 */

public class DlgNuevoCriterio extends DialogFragment {

    TextView tituloCriterio;
    GestorModeloAHP gestorModelo;
    Communicator communicator;
    CriterioDefinicionAdapter adapter;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        communicator = (Communicator) activity;
    }

    public CriterioDefinicionAdapter getAdapter() {
        return adapter;
    }

    public void setAdapter(CriterioDefinicionAdapter adapter) {
        this.adapter = adapter;
    }


    public void setGestorModelo() {
        this.gestorModelo =(GestorModeloAHP) getActivity().getApplication();
    }

    public static DlgNuevoCriterio newInstance(CriterioDefinicionAdapter ad) {

        DlgNuevoCriterio frag = new DlgNuevoCriterio();

        frag.setAdapter(ad);
        return frag;
    }
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        LayoutInflater inflater = getActivity().getLayoutInflater();
        View v = inflater.inflate(R.layout.step_1_dlg_nvo_crit,null);


       gestorModelo = (GestorModeloAHP) getActivity().getApplication();

       // Item criterio = gestorModelo.getItemById(idCriterio,true);
        final EditText txtCriterio = (EditText) v.findViewById(R.id.txtNuevoCriterioNombre2);

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        //Obtengo el inflater

        //Inflar y setear el layout.
        builder.setView(v)
                //Botones.
                .setPositiveButton(R.string.action_next,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                                int whichButton) {
                                String nombre = txtCriterio.getText().toString();
                                gestorModelo.addItem(nombre, true);
                                //adapter.notifyDataSetChanged();
                                //communicator.agregar(nombre);

                                dismiss();

                            }
                        })

                .setNegativeButton(R.string.action_previous,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                                int whichButton) {
                                dismiss();
                            }
                        });
        return builder.create();
    }

    public GestorModeloAHP getGestorModelo() {
        return gestorModelo;
    }


    public interface Communicator{
        public void agregar(String nombre);
    }
}
